const gulp=require("gulp");
const htmlmin=require("gulp-htmlmin");
gulp.task("htmlmin",()=>{
    gulp.src(["./src/**/*.html","./src/**/*.htm"])
    .pipe(htmlmin({
        collapseWhitespace:true,
        removeStyleLinkTypeAttributes:true,//删除无关属性
        removeComments:true,//删除注释
        removeEmptyElements:true
    }))
    .pipe(gulp.dest("./dist/"));
})

var sass=require("gulp-sass");
sass.compiler=require("node-sass");
gulp.task("sass",()=>{
    return gulp.src("./src/sass/**/*.scss")
    .pipe(sass({
        outputStyle:"compressed"
    }).on("error",sass.logError))
    .pipe(gulp.dest("./src/css/"));

});


const css=require("gulp-clean-css");
gulp.task("css",()=>{
    gulp.src("./src/css/**/*.css")
    .pipe(css())
    .pipe(gulp.dest("./dist/css"));

})

const minjs=require("gulp-uglify");
const babel=require("gulp-babel");
gulp.task("minjs",function(){
    gulp.src(["./src/js/**/*.js"])
    .pipe(babel())
    .pipe(minjs())
    .pipe(gulp.dest("./dist/js/"))
})

const imagemin=require("gulp-imagemin");
gulp.task("imagemin",()=>{
    gulp.src(["./src/images/**/*.{png,jpg,svg,jpeg,gif}"])
    .pipe(imagemin())
    .pipe(gulp.dest("./dist/images/"))
});


gulp.task("sass:watch",function(){
    gulp.watch("./src/sass/**/*.scss",["sass","css"]);

    gulp.watch("./src/**/*.html",["htmlmin"]);

    gulp.watch("./src/css/**/*.css",["css"]);

    gulp.watch("./src/js/**/*.js",["minjs"]);


})

gulp.task("default",["htmlmin","sass","css","imagemin","sass:watch","minjs"]);
